home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / program / swags_z.zip / STRINGS.SWG / 0088_PosIn().pas < prev    next >
Pascal/Delphi Source File  |  1994-08-24  |  4KB  |  91 lines

  1. {
  2.  Here's a routine that's faster than Pos on my system. It's written
  3.  in external assembly language, and linked directly into TP program
  4.  code.  I'm including an example of using the code, the assembly
  5.  source code, and a pre-assembled ready-to-compile POSIN.OBJ file:
  6.  
  7.  Here's the example... }
  8.  
  9. (*******************************************************************)
  10. PROGRAM Demo; { A faster Pos() for TP4+. June 17/94 Greg Vigneault  }
  11.  
  12. VAR str : STRING;  j : BYTE;
  13.  
  14. FUNCTION PosIn (Pattern, Str : STRING) : BYTE; EXTERNAL;
  15. {$L POSIN.OBJ}    (* link in the external code *)
  16.  
  17. BEGIN
  18.       WriteLn;
  19.       str := 'Position of THIS in string is ';
  20.       j := PosIn ('THIS',str);;  WriteLn (str,j);
  21.       WHILE (j > 1) DO BEGIN Write (' '); DEC(j); END;
  22.       WriteLn ('^^^^');
  23.       WriteLn;
  24. END.
  25. (*******************************************************************)
  26.  
  27. Here's the assembly code source...
  28. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  29. code    segment byte public 'CODE'
  30.         assume  cs:code
  31. ; FUNCTION PosIn (pattern, string : STRING) : BYTE;
  32. pattern equ dword ptr 8[bp]
  33. string  equ dword ptr 4[bp]
  34. PosIn   proc    near
  35.         public  PosIn
  36.         push  bp                    ; preserve
  37.         mov   bp, sp
  38.         push  ds
  39.         push  es
  40.         cld                         ; assure forward scans
  41.         lds   si, pattern           ; DS:SI -> pattern
  42.         sub   ax, ax                ; zero
  43.         lodsb                       ; get length byte
  44.         test  ax, ax                ; null string?
  45.         jz    done                  ; yes: exit with zero
  46.         mov   dx, ax                ; length of pattern
  47.         les   di, string            ; ES:DI -> string
  48.         sub   bx, bx                ; zero
  49.         mov   bl, es:[di]           ; string length
  50.         cmp   bx, dx                ; pattern > string ?
  51.         jc    none                  ; yes: exit with zero
  52.         inc   di                    ; point to 1st string char
  53.         lodsb                       ; get pattern 1st char
  54.         dec   dx                    ; adjust pointer
  55.         sub   bx, dx                ; don't need to check end
  56.   po0:  mov   cx, bx                ; unsearched chars count
  57.         repne scasb                 ; search for pattern char
  58.         jne   none                  ; no char match
  59.         mov   bx, cx                ; unsearched count
  60.         push  di                    ; save text pointers
  61.         push  si
  62.         mov   cx, dx                ; length of pattern
  63.         repe  cmpsb                 ; check for pattern
  64.         pop   si                    ; restore pointers
  65.         pop   di
  66.         jne   po0                   ; loop if no pattern match
  67.         lds   ax, string            ; string pointer
  68.         xchg  ax, di                ; swap offsets
  69.         sub   ax, di                ; subtract offsets
  70.         dec   ax                    ; adjust for PosIn
  71.         jmp   short done            ; found pattern
  72.   none: sub   ax, ax                ; pattern not found
  73.   done: pop   es                    ; restore
  74.         pop   ds
  75.         mov   sp, bp
  76.         pop   bp
  77.         ret   8
  78. PosIn   endp
  79. code    ends
  80.         end
  81. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  82.  
  83. USE XX3402 to decode this and obtain POSIN.OBJ requried for this unit.
  84.  
  85. *XX3402-000140-170694--72--85-37398-------POSIN.OBJ--1-OF--1
  86. U+g+0L-jQqZi9Y3HHHGK-k++-2BDF2J2a+Q+82U++U6-v7+A+++--J-DIoZC++++pMU2++0W
  87. +R4UH++-++-JWykS-jn3RUUfk8m3k5EkWx12TUEfqmO85HjOQW-5f2cfqcj9wetp3MjNJpO9
  88. mjCaLZxpvgJ4-7QfloXf+Wj+-ly9tJr00+1nWU6++5E+
  89. ***** END OF BLOCK 1 *****
  90.  
  91.